package org.hackystat.app.projectstatus.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import org.hackystat.core.kernel.admin.ServerProperties;

/**
 * Provides the singleton timer task that runs each day at the time chosen. This value can be 
 * overriden by the attribute 'hackystat.dailyalert.hour' in hackystat.properties file.
 * To perform an action once a day at 2am, implement the DailyTimerTask interface and
 * get yourself scheduled for invocation with DailyTimerTaskManager.getInstance().addTask().
 * Currently, the right place to add yourself is in ServerStartup, but eventually we will create an
 * XML-based approach to allow extensions to add themselves to the DailyTimerTask.
 *
 * @author    Josh Stupplebeen
 * 
 */
public class ProjectStatusAlertScheduler {

  /** Timer object. */
  private Timer timer = null;
  /** Singleton instance of DailyTimer. */
  private static ProjectStatusAlertScheduler theInstance;
  /** Milliseconds in a day. */
  private long millisecondsInADay = (1000 * 60 * 60 * 24);
  /** Task manager. */
  private ProjectStatusTimerTask projectStatusTimerTask;
  /** Current Date. */
  private Date date = new Date();

  /** Private constructor for creating the singleton Timer instance. */
  private ProjectStatusAlertScheduler() {
    this.projectStatusTimerTask = new ProjectStatusTimerTask();
    this.timer = new Timer(true);
    this.timer.schedule(this.projectStatusTimerTask, this.getNextAlertTime(),
        this.millisecondsInADay);
  }

  /**
   * Returns the TimerTaskManager associated with this timer.
   *
   * @return   The TimerTaskManager.
   */
  public ProjectStatusTimerTask getProjectStatusTimerTask() {
    return this.projectStatusTimerTask;
  }

  /**
   * Create a date corresponding to the next time at which daily alert email will be sent..
   * 
   * @return   The next scheduled alert time.
   */
  private Date getNextAlertTime() {
    int currentHour = ServerProperties.getInstance().getDailyAlertHour();
    if (currentHour < 0 || currentHour > 23) {
      String msg = "ProjectStatusAlertTimer::getTomorrowTime() Invalid parameter " + currentHour
          + " detected, default value 2 is used.";
      ServerProperties.getInstance().getLogger().warning(msg);
      currentHour = 2;
    }
    ServerProperties.getInstance().getLogger().fine(
        "hackystat.projectstatusalerttimer.hour = " + currentHour);
    Calendar cal = Calendar.getInstance();
    //    cal.add(Calendar.DAY_OF_MONTH, 1);
    //    cal.set(Calendar.HOUR_OF_DAY, currentHour);
    //    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 30);
    //    cal.set(Calendar.MILLISECOND, 0);
    System.out.println(" -- Get next alert time -- ");
    System.out.println("The next alert will occur at: ");
    System.out.println(cal.getTime().toString());
    return cal.getTime();
  }

  /**
   * Returns the singleton DailyTimer instance.
   *
   * @return   The instance value
   */
  public static synchronized ProjectStatusAlertScheduler getInstance() {
    if (ProjectStatusAlertScheduler.theInstance == null) {
      ServerProperties.getInstance().getLogger().fine("Starting up ProjectStatusAlertTimer task.");
      ProjectStatusAlertScheduler.theInstance = new ProjectStatusAlertScheduler();
    }
    return ProjectStatusAlertScheduler.theInstance;
  }

}
